﻿/// <Summary> 
/// $Author: Nikhil Chinchwade 
/// This file contains class representing Performance Counter

/// </summary>
/// 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MindTree.PerfWorkbench.Azure.Monitor.Core
{

    /// <summary>
    /// Class representing Performance Counter
    /// </summary>
    public class PerfCounter
    {

        private string machineName;

        /// <summary>
        /// Name of the Machine (Azure Role Instance)
        /// </summary>
        public string MachineName
        {
            get
            {
                return machineName;
            }
            set
            {
                machineName = value;
            }
        }

        private string category;

        /// <summary>
        /// Counter Category
        /// </summary>
        public string Category
        {
            get
            {
                return category;
            }
            set
            {
                category = value;
            }
        }

        private string counterName;

        /// <summary>
        /// Counter Name
        /// </summary>
        public string CounterName
        {
            get
            {
                return counterName;
            }
            set
            {
                counterName = value;
            }
        }

        private string instance;

        /// <summary>
        /// Counter Instance
        /// </summary>
        public string Instance
        {
            get
            {
                return instance;
            }
            set
            {
                instance = value;
            }
        }

        /// <summary>
        /// Default constructor
        /// </summary>
        public PerfCounter()
        {
        }

        /// <summary>
        /// Parameterised Constructor
        /// </summary>
        /// <param name="paramCopyCounter">counter</param>
        public PerfCounter(PerfCounter paramCopyCounter)
        {
            this.MachineName = paramCopyCounter.MachineName;
            this.Category = paramCopyCounter.Category;
            this.CounterName = paramCopyCounter.CounterName;
            this.Instance = paramCopyCounter.Instance;
        }

        /// <summary>
        /// public constructor
        /// </summary>
        /// <param name="paramCategory">Performance Counter Category</param>
        /// <param name="paramCounterName">Performance Counter Name </param>
        /// <param name="paramInstance">Performance Counter Instance</param>
        public PerfCounter(string paramMachineName, string paramCategory, string paramCounterName, string paramInstance)
        {
            MachineName = paramMachineName;
            Category = paramCategory;
            CounterName = paramCounterName;
            Instance = paramInstance;
        }

        /// <summary>
        /// Create a counter instance provided its string representation
        /// </summary>
        /// <param name="paramCounter">Performance Counter Name</param>
        /// <returns>Performance Counter Object</returns>
        public static PerfCounter GetCounterFromString(string paramMachineName, string paramCounter)
        {

            PerfCounter tempCounter = null;
            if (ValidateStringCounterFormat(paramCounter))
            {
                tempCounter = new PerfCounter();
                if (null != paramMachineName)
                {
                    tempCounter.MachineName = paramMachineName;
                }
                string[] countersParts = paramCounter.Split('\\');
                if (countersParts.Length > 2)
                {
                    if (countersParts[1].Contains('('))
                    {
                        int initindex = countersParts[1].IndexOf('(');
                        int endindex = countersParts[1].IndexOf(')');

                        tempCounter.Category = countersParts[1].Substring(0, initindex);
                        tempCounter.Instance = countersParts[1].Substring(initindex + 1, endindex - initindex - 1);
                        tempCounter.CounterName = countersParts[2];
                    }
                    else
                    {
                        tempCounter.Category = countersParts[1];
                        tempCounter.CounterName = countersParts[2];
                        tempCounter.Instance = "";
                    }
                }
                if (countersParts.Length == 2)
                {
                    if (countersParts[0].Contains('('))
                    {
                        int initindex = countersParts[0].IndexOf('(');
                        int endindex = countersParts[0].IndexOf(')');

                        tempCounter.Category = countersParts[0].Substring(0, initindex);
                        tempCounter.Instance = countersParts[0].Substring(initindex + 1, endindex - initindex - 1);
                        tempCounter.CounterName = countersParts[1];
                    }
                    else
                    {
                        tempCounter.Category = countersParts[0];
                        tempCounter.CounterName = countersParts[1];
                        tempCounter.Instance = "";
                    }
                }
            }
            
            return tempCounter;
        }

        /// <summary>
        /// Not implemented now
        /// </summary>
        /// <param name="paramCounter"></param>
        /// <returns></returns>
        public static bool ValidateStringCounterFormat(string paramCounter)
        {
            return true;
        }

        /// <summary>
        /// Get the String Representation of the current Performance Counter object
        /// </summary>
        /// <returns>String representation of the counter</returns>
        /// 
        public override string ToString()
        {
            if (String.IsNullOrEmpty(Instance))
            {
                if (String.IsNullOrEmpty(MachineName))
                {
                    return "\\" + Category + "\\" + CounterName;
                }
                else
                {
                    return "\\\\" + MachineName + "\\" + Category + "\\" + CounterName;
                }
            }
            else
            {
                if (String.IsNullOrEmpty(MachineName))
                {
                    return "\\" + Category + "(" + Instance + ")" + "\\" + CounterName;
                }
                else
                {
                    return "\\\\" + MachineName + "\\" + Category + "(" + Instance + ")" + "\\" + CounterName;
                }
            }           
          
        }

        /// <summary>
        /// Equals method to check equality of two Performance Counter objects
        /// </summary>
        /// <param name="obj">Performance Counter Object</param>
        /// <returns>Boolean</returns>
        public override bool Equals(object obj)
        {

            PerfCounter tempCounter = (PerfCounter)obj;
            if (null != tempCounter.MachineName && null != MachineName)
            {
                if ((tempCounter.MachineName.Equals(MachineName)) &&(tempCounter.category.Equals(Category)) && (tempCounter.counterName.Equals(CounterName)) && (tempCounter.instance.Equals("*") || Instance.Equals("*") || tempCounter.instance.Equals(Instance)))
                {
                    return true;
                }
            }
            else       
            {
                if ((tempCounter.category.Equals(Category)) && (tempCounter.counterName.Equals(CounterName)) && (tempCounter.instance.Equals("*") || Instance.Equals("*") || tempCounter.instance.Equals(Instance)) )
                {
                    return true;   
                }
            }
            return false;
        }
                
        #region ICloneable Members
        /// <summary>
        /// Clone the current Performance Counter object
        /// </summary>
        /// <returns>Performance Counter object</returns>
        public object  Clone()
        {
            return new PerfCounter(this.MachineName,this.Category, this.CounterName, this.Instance);
        }

        #endregion

    }
}
